package com.dhc.appbusinessfrm.view.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dhc.appbusinessfrm.R;
import com.dhc.appbusinessfrm.ioc.presenter.LoginPresenterModule;
import com.dhc.appbusinessfrm.presenter.LoginPresenter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

@EActivity(R.layout.layout_login)
public class LoginActivity extends BaseActivity implements LoginInteractor {

    @Inject
    LoginPresenter loginPresenter;

    @ViewById(R.id.edtLoginName)
    EditText edtLoginName;

    @ViewById(R.id.edtPassword)
    EditText edtPassword;

    @ViewById(R.id.btnLogin)
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected List<Object> getModules() {
        return Arrays.<Object>asList(new LoginPresenterModule(this));
    }

    @AfterViews
    void init() {
        loginPresenter.ini();
    }

    @Click(R.id.btnLogin)
    void doLogin() {
        loginPresenter.doLogin();
    }

    @Override
    public void setEdtLoginName(String name) {
        edtLoginName.setText(name);
    }

    @Override
    public void setListener() {
        edtPassword
                .setOnEditorActionListener(new TextView.OnEditorActionListener() {

                    @Override
                    public boolean onEditorAction(TextView v, int actionId,
                                                  KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE
                                || actionId == EditorInfo.IME_ACTION_NONE) {
                            doLogin();
                        }
                        return false;
                    }
                });
    }

    @Override
    public String getLoginName() {
        return edtLoginName.getText().toString();
    }

    @Override
    public String getPassword() {
        return edtPassword.getText().toString();
    }
}
